//
//  ViewController.m
//  UI19_XML数据解析作业
//
//  Created by dllo on 16/4/5.
//  Copyright © 2016年 付冬梅. All rights reserved.
//

#import "ViewController.h"
#import "CartoonModel.h"
#import "SAXXMLParser.h"
#import "SAXTableView.h"
#import "SAXTableViewCell.h"

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) SAXTableView *saxTableView;
@property (nonatomic, retain) NSArray *dataSourceArr;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.title = @"SAXXML解析";
    self.navigationController.automaticallyAdjustsScrollViewInsets = NO;
    
    [self createTableView];
    [self dataHandle];
}

- (void)createTableView {
    self.saxTableView = [[SAXTableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    self.saxTableView.delegate = self;
    self.saxTableView.dataSource = self;
    self.saxTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.saxTableView];
    [self.saxTableView registerClass:[SAXTableViewCell class] forCellReuseIdentifier:@"sax"];
}

- (void)dataHandle {
    self.dataSourceArr = [SAXXMLParser saxParserWithFileName:@"Cartoon" fileType:@"xml"];
    NSLog(@"%@", self.dataSourceArr);
    [self.saxTableView reloadData];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CartoonModel *model = self.dataSourceArr[indexPath.row];
    
    return [SAXTableViewCell heightOfCellWithText:model.desc];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SAXTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"sax"];
    CartoonModel *model = self.dataSourceArr[indexPath.row];
    [cell setCartoonModel:model];
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
